# LEVEL MAPPING

## A. locate the level's map data

1. LOAD LEVEL BYTE
   example: 0x10 (Mario's Pad)
   - this is most often loaded from the exit field data, but can also be loaded using the event instruction 0x68
   - document reference: doc_areas.txt
2. CALCULATE LAYOUT POINTER: (byte * 0x12) + 1D0040
   example: (0x10 * 0x12) + 0x1D0040 = 1D0160
   - this is the root class for defining an area
   - byte 1 is the pointer to the mapping data for the level
   - several levels can use the same mapping data, if they are similar in appearance or function (eg.townplaces, grasslands, etc.)
   - all of the other bytes are variables that can change certain orientations of the level (eg.layer motion, level masks, level cropping, etc.)
   - document reference: doc_area-layout.txt
3. CALCULATE MAP POINTER: (byte 1 * 0x0F) + 1D2440
   example: (0x06 * 0x0F) + 1D2440 = 1D249A
   - document reference: doc_area-maps.txt

## B. determine the level's graphic sets

1. DEFINE GRAPHIC SETS
   - graphic sets are listed by byte order in doc_area-maps.txt under Tileset Group Values
   - there are 5 sets for each level map
2. CALCULATE COMPRESSED GRAPHICS POINTER: use "difference method" for trans-bank location
   - compressed graphics are located in banks 0A0000 through 140000
   - to determine the bank and pointer of the compressed graphics use this equation: (byte + 0x48) * 2
     example: (0x40 + 0x48) * 2 = 0x0110
     1. the "difference method" compares the pointer (0x0110 in this case) to the first byte in bank 0A0000
     2. only if it is greater than/equal to the first byte in bank 0A0000, the pointer is subtracted from the value (which is true in this case)...
        ...otherwise, it adds the pointer to 0A0000 and the sum is the location of the compressed graphics (not so in this case)
     3. step 2 is repeated for each bank until the value is less than the first byte in the current bank
     0x0110 - 0x009C = 0x0074
     0x0074 - 0x0020 = 0x0054
     0x0054 - 0x0022 = 0x0032
     0x0032 - 0x0024 = 0x000E
     0x000E < 0x0024, therefore...
     ...0x000E + 0E0000 = 0E000E
   the final difference is added to the current bank, where the sum is the location of the compressed graphics pointer

the grasslands BG layer 1 tileset

## C. determine the level's tilesets

1. DEFINE TILESETS
   - tilesets are listed by byte order in doc_map-tiles.txt
   - there is a tileset for BG layer 1, 2 and 3 (0x06 and 0xBE, Mario's Pad doesn't use BG layer 3)
2. CALCULATE TILESET POINTER
   - compressed tilesets are located in banks 3B0000 through 3D0000 and both BG layer 1 and 2 are almost always consecutive
   - to determine the bank and pointer of the compressed tilesets use this equation: [(byte AND 0x7F) + 0x20] * 2
     BG layer 1 example: [(0x06 AND 0x7F) + 0x20] * 2 = 0x4C
     BG layer 2 example: [(0xBE AND 0x7F) + 0x20] * 2 = 0xBC
     - use the same "difference method" as used with the graphic sets pointer, starting with bank 3B0000
     BG layer 1:
     0x004C < 0x0074, therefore...
     ...0x004C + 3B0000 = 3B004C
     BG layer 2:
     0x00BC - 0x0074 = 0x0048
     0x0048 - 0x0042 = 0x0006
     0x0006 < 0x0044, therefore...
     ...0x0006 + 3D0000 = 3D0006

- the long image on the right is the complete BG layer 1 tileset for grasslands areas
- the BG layer 2 tileset is very similar, in that it organizes many of the same tiles yet in a different order
- tilesets assume the assigned palette set for each area that uses the tileset
- tiles in tilesets are organized in the order they appear in the decompressed data
- tilemap byte values read from the tilesets to display the tile defined by the tileset data
- the tilemap byte values read from the order of the tileset's data

The tileset organization tutorial explains how the tiles in a tileset are defined.

## D. determine the level's tilemaps

1. DEFINE TILEMAPS
   - tilemaps are listed by byte order in doc_map-tiles.txt
   - there is a tilemap for BG layer 1, 2, and 3 (0x0C and 0x0D, Mario's Pad doesn't use BG layer 3)
2. CALCULATE TILEMAP POINTER
   - compressed tilemaps are located in banks 160000 through 1A0000 and both BG layer 1 and 2 are almost always consecutive
   - to determine the bank and pointer of the compressed tilesets use this equation: (byte + 0x40) * 2
     BG layer 1 example: (0x0C + 0x40) * 2 = 0x98
     BG layer 2 example: (0x0D + 0x40) * 2 = 0x9A
     - use the "difference method" again starting with bank 160000
     BG layer 1:
     0x0098 < 0x00DA, therefore...
     ...0x0098 + 160000 = 160098
     BG layer 2:
     0x009A < 0x00DA, therefore...
     ...0x009A + 160000 = 16009A

Mario's Pad BG layer 1 tilemap

- A tilemap encompasses a region
- A region is a set of areas that share a similar style. An example would be half of the areas in Booster's Tower.
- Every region's map is 1024x1024 pixels, or 64x64 tiles.
- A tile byte is 2 bytes long, and a row is 0x80 bytes (64 tiles).
- The tile bytes correspond to the order they are formatted in the tileset, as seen directly in the tileset image.
- The tilemap gathers these tiles and arranges them into a map of BG layer 1 of Mario's Pad. BG layer 2 uses a different tileset but with a very similar set of tiles.
- The tilemap image to the right is essentially the actual map of BG layer 1 of Mario's Pad. Mario's Pad has its own tilemap and isn't grouped into a "region", like some Mushroom Way areas for example.